package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFStringWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFStringWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFStringWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFStringWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFStringWidget widget) {
        ObjectNode ui = node.getUi();
        putString(ui, "type", widget.type());
        putString(ui, "placeholder", widget.placeholder());
        ui.put("autocomplete", widget.autocomplete().value());
        ui.put("autofocus", widget.autofocus());
        putString(ui, "addOnBefore", widget.addOnBefore());
        putString(ui, "addOnAfter", widget.addOnAfter());
        putString(ui, "addOnBeforeIcon", widget.addOnBeforeIcon());
        putString(ui, "addOnAfterIcon", widget.addOnAfterIcon());
        putString(ui, "prefix", widget.prefix());
        putString(ui, "prefixIcon", widget.prefixIcon());
        putString(ui, "suffix", widget.suffix());
        putString(ui, "suffixIcon", widget.suffixIcon());
        ui.put("borderless", widget.borderless());
    }

    @Override
    public Class[] supports() {
        return new Class[]{SFStringWidget.class};
    }
}
